<template name="textMsg">
	<view class="msgRow" :class="item.flow">
		<image class="userAvatar" :src="item.flow=='in'?toInfo.avatar:myInfo.avatar"></image>
		<view class="msgBox" :class="item.type">
			<view class="msgCnt">
				<block v-for="(itm,idx) in textDom" :key="idx">
					<text v-if="itm.name=='text'">{{itm.text}}</text>
					<image class="emoji" v-if="itm.name=='img'" :src="itm.src"></image>
				</block>
			</view>
			<view class="jiantou"></view>
		</view>
	</view>
</template>

<script>
	import {
		emojiMap,
		emojiUrl,
		emojiName
	} from '@/sdk/timWx/emojiMap.js';
	export default{	
		name:'textMsg',		//组件名称
		// 传递参数
		props:{
			item:{
				type:Object,
				default:null
			},
			myInfo:{
				type:Object,
				default:null
			},
			toInfo:{
				type:Object,
				default:null
			}
		},
		// 组件参数
		data(){
			return {
				emojiName: emojiName,
				emojiMap: emojiMap,
				emojiUrl: emojiUrl,
				textDom:null
			}
		},
		// 组件方法
		methods:{
			// 消息中的Emoji字符串转表情
			parseText(payload) {
				let renderDom = []
				// 文本消息
				let temp = payload.text
				let left = -1
				let right = -1
				while (temp !== '') {
					left = temp.indexOf('[')
					right = temp.indexOf(']')
					switch (left) {
						case 0:
							if (right === -1) {
								renderDom.push({
									name: 'text',
									text: temp
								})
								temp = ''
							} else {
								let _emoji = temp.slice(0, right + 1)
								if (emojiMap[_emoji]) { // 如果您需要渲染表情包，需要进行匹配您对应[呲牙]的表情包地址
									renderDom.push({
										name: 'img',
										src: emojiUrl + emojiMap[_emoji]
									})
									temp = temp.substring(right + 1)
								} else {
									renderDom.push({
										name: 'text',
										text: '['
									})
									temp = temp.slice(1)
								}
							}
							break
						case -1:
							renderDom.push({
								name: 'text',
								text: temp
							})
							temp = ''
							break
						default:
							renderDom.push({
								name: 'text',
								text: temp.slice(0, left)
							})
							temp = temp.substring(left)
							break
					}
				}
				return renderDom
			},
		},
		beforeCreate() {
			
		},
		created() {
			this.textDom=this.parseText(this.item.payload)
		}
	}
</script>

<style lang="less">
	.msgRow {
		display: flex;
		align-items: flex-start;
		padding-right: 118upx;
		margin-bottom: 20upx;
	
		.userAvatar {
			width: 86upx;
			height: 86upx;
			border-radius: 50%;
			margin-right: 32upx;
			flex-shrink: 0;
		}
	
		.msgBox {
			background-color: #FFFFFF;
			border-radius: 10upx;
			position: relative;
			padding: 20upx 20upx;
			display: flex;
			align-items: center;
			min-height: 86upx;
	
			.jiantou {
				width: 20upx;
				height: 20upx;
				background-color: #FFFFFF;
				border-radius: 4upx;
				position: absolute;
				top: 43upx;
				left: -10upx;
				transform: translateY(-50%) rotate(45deg);
			}
	
			.msgCnt {
				font-size: 28upx;
				display: flex;
				align-items: center;
	
				.emoji {
					width: 36upx;
					height: 36upx;
				}
			}
		}
	
		.imgMsg {
			flex-grow: 0;
			overflow: hidden;
			padding: 0;
		}
	}
	.out {
		padding-left: 118upx;
		padding-right: 0;
		justify-content: flex-end;
	
		.userAvatar {
			order: 2;
			margin-right: 0;
			margin-left: 32upx;
		}
	
		.msgBox {
			order: 1;
	
			.jiantou {
				right: -10upx;
				left: auto;
			}
		}
	}
	.orderMsg {
		display: flex;
		flex-direction: column;
		flex-grow: 1;
		align-items: flex-start !important;
	
		.orderTishi {
			font-size: 24rpx;
			color: #8e8e8e;
			margin-bottom: 20upx;
		}
	
		.goodBox {
			display: flex;
			background-color: #f6f6f6;
			border-radius: 10upx;
			width: 100%;
			image {
				width: 146upx;
				height: 146upx;
				border-radius: 10upx;
				flex-shrink: 0;
				margin-right: 18upx;
			}
	
			.goodInfo {
				display: flex;
				flex: 1;
				flex-direction: column;
				justify-content: space-between;
				padding: 5upx 5upx 5upx 0;
	
				.goodTitle {
					font-size: 32rpx;
					color: #222433;
				}
	
				.paymentInfo {
					display: flex;
	
					.paymentTishi {
						font-size: 24upx;
					}
	
					.paymentCount {
						font-size: 30rpx;
						color: #ed2e2e;
					}
				}
			}
		}
	
		.orderOp {
			width: 100%;
			display: flex;
			justify-content: flex-end;
			margin-top: 18upx;
	
			button {
				margin: 0;
				height: 62upx;
				border-radius: 31upx;
				border: none;
				background: linear-gradient(to right, #ff6829, #ff2f36);
				color: #FFFFFF;
				padding: 0 50upx;
				line-height: 62upx;
				font-size: 28upx;
				font-weight: bold;
			}
	
			button:after {
				border: none;
			}
		}
	}
	
</style>
